package cn.kgc.crowdfundingadmin2.service;

import cn.kgc.crowdfundingadmin2.mapper.RoleMapper;
import cn.kgc.crowdfundingadmin2.pojo.Role;
import cn.kgc.crowdfundingadmin2.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Role> roles = roleMapper.selectByExample(example);
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return pageInfo;
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);
        return roleMapper.selectByExample(example);
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);

        roleMapper.deleteByExample(example);
    }

    @Override
    public void saveRole(String roleName) {
        roleMapper.insert(new Role(null,roleName));
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {
        roleMapper.deletAdminRelationRole(adminId);
        if (roleIdList!=null){
            roleMapper.insertAdminRelationRole(adminId,roleIdList);
        }
    }
}
